<?php
	class ParserController extends Controller
	{
		/**
	 	 * page title
	 	 */
		public $page_title = LANG_PARSER;

		/**
	 	 * name
	 	 */
		public $name = 'parser';
		
		/**
	 	 * page layout
	 	 */
		public $layout = 'main';
		
		/**
	 	 * necessary helpers
	 	 */
		public $helpers = array ('html', 'htmlx');
		
		/**
	 	 * necessary javascript files
	 	 */
		public $javascript = array("TreeMenu");
		
		/**
	 	 * necessary components
	 	 */
		public $components = array ("TreeMenu", 'htmlparser/html_parser');
		
		/**
		 * Header enveloper
		 *
		 * @var string
		 */
		private $headerSeparator = 'HEADER';

		/**
		 * Footer enveloper
		 *
		 * @var string
		 */
		private $footerSeparator = 'FOOTER';
		
		/**
		 * Parsed pages folder
		 *
		 * @var string
		 */
		private $parsedFolder = './design/parsed';
		
		/**
		 * Displays the design page for the parsing process
		 *
		 */
		function Design()
		{
			$this->data[path_to_file] = "./design/index.html";
			$this->data[file] = file_get_contents($this->data[path_to_file]);
		}
		
		/**
		 * Saves the file, backups the old file version in the ./backup folder and parses the new file content into parts
		 *
		 */
		function SaveFile()
		{
			//creates the file with the specified name and backups the old one
		    copy($this->request->post[path_to_file], $_SERVER['DOCUMENT_ROOT'].'/app/spanel_backup/'.pathinfo($this->request->post[path_to_file], PATHINFO_BASENAME).'_'.date('Y_m_d_H_i_s').'.bak');
			$fp = fopen($this->request->post[path_to_file], 'w+');

			//writes to the file
			$content = iconv('UTF-8', 'windows-1251', $this->request->post[content]);
			fwrite($fp, $content);
			
			//closes the file
			fclose($fp);				

			$this->ParseDesign($this->request->post[path_to_file]);
			
			exit();
		}
		
		/**
		 * Parses design into separate framework files
		 *
		 * @param string $content file contents
		 */
		function ParseDesign($path_to_file)
		{
			$sourceContent = file($path_to_file);
			$lines = count($sourceContent);
			
			$headContent = array();
			$headerContent = '';
			$footerContent = '';
			
			for ($i = 1; $i < $lines; $i++)
			{
				//gets the trimmed line
				$trimLine[$i] = trim($sourceContent[$i]);
				
				//head
				if ($isHead)
				{
					$headContent[] = $sourceContent[$i];
					if (ereg("^(</head>)", $trimLine[$i]))
					{
						$isHead = false;
						continue;
					}
				}
				//if it is head beginning
				else if (ereg("^(<head>)", $trimLine[$i]))
				{
					$isHead = true;
					$headContent[] = $sourceContent[$i];
					continue;
				}
												
				//HEADER
				//if it is header content
				else if ($isHeader)
				{
					$headerContent .= $sourceContent[$i];
					if ($trimLine[$i] == '<!--/'.$this->headerSeparator.'-->')
					{
						$isHeader = false;
						continue;
					}
				}
				//if it is header beginning
				else if ($trimLine[$i] == '<!--'.$this->headerSeparator.'-->')
				{
					$isHeader = true;
					$headerContent .= $sourceContent[$i];
					continue;
				}
				
				//FOOTER
				//if it is footer content
				else if ($isFooter)
				{
					$footerContent .= $sourceContent[$i];
					if ($trimLine[$i] == '<!--/'.$this->footerSeparator.'-->')
					{
						$isFooter = false;
						continue;
					}
				}
				//if it is footer beginning
				else if ($trimLine[$i] == '<!--'.$this->footerSeparator.'-->')
				{
					$isFooter = true;
					$footerContent .= $sourceContent[$i];
					continue;
				}
				
			}
			
			//creates al necessary folders
			mkdir($this->parsedFolder.'/elements');
			mkdir($this->parsedFolder.'/images');
			mkdir($this->parsedFolder.'/layouts');
			
			//layout content
			$layoutContent = $this->GenerateLayout($headContent, $path_to_file);
						
			//writes layout content
			$layoutFile = fopen($this->parsedFolder.'/layouts/default.php', 'w+');
			fwrite($layoutFile, $layoutContent);
			fclose($layoutFile);
			
			//writes header file
			$headerFile = fopen($this->parsedFolder.'/elements/header.php', 'w+');
			fwrite($headerFile, $headerContent);
			fclose($headerFile);
			
			//writes footer file
			$footerFile = fopen($this->parsedFolder.'/elements/footer.php', 'w+');
			fwrite($footerFile, $footerContent);
			fclose($footerFile);
		}
		
		/**
		 * Generates layout page
		 *
		 * @param array $headContent head section content
		 * @return string generated layout page
		 */
		private function GenerateLayout($headContent, $path_to_file)
		{
			$layoutContent = '';
			
			$layoutContent .= "<html>\n<head>\n<?php\n\tglobal \$object, \$current_controller;\n";
			
			//parses HTML head content
			$headContent = $this->ParseHTMLHead($headContent, $path_to_file);
			
			//adds head content to layout content
			$layoutContent .= $headContent;

			$layoutContent .= "?>\n</head>\n";
			
			$layoutContent .= "</html>";
			
			return $layoutContent;
		}
		
		/**
		 * Parses HTML content
		 *
		 * @param array $content content in array
		 * @return parsed content
		 */
		private function ParseHTMLHead(array $headLines = array(), $path_to_file)
		{
			$content = '';
			
			include_once('./app/includes/components/htmlparser/html_parser.php');
			$parsedHead = new HtmlParser($headLines);
			//$content = serialize($parsedHead->contentLines);
			
			foreach ($parsedHead->contentLines as $line)
			{
				switch ($line[tag])
				{
					case 'meta':
						$content .= $this->GenerateMetaTag($line);
						break;
					case 'link':
						$content .= $this->GenerateLinkTag($line, $path_to_file);
						break;
					case 'script':
						$content .= $this->GenerateScriptTag($line);
						break;
					case 'title':
						$content .= $this->GenerateTitleTag($line);
						break;
				}
			}
			//$_SESSION[test][] = $content;
			
			return $content;
		}
		
		/**
		 * Generates meta tag
		 *
		 * @param array $line tag array to be parsed
		 * @return generated string
		 */
		private function GenerateMetaTag(array $line)
		{
			if (isset($line['attributes']['http-equiv']))
			{
				$result = "\t\$current_controller->helpers->html->MetaTag('{$line[attributes]["http-equiv"]}', '{$line[attributes][content]}', 'http-equiv');";
			}
			else
			{
				$result = "\t\$current_controller->helpers->html->MetaTag('{$line[attributes][name]}', '{$line[attributes][content]}');";
			}
			return $result."\n";	
		}
		
		/**
		 * Generates link tag
		 *
		 * @param array $line tag array to be parsed
		 * @return generated string
		 */
		private function GenerateLinkTag(array $line, $path_to_file)
		{
			switch ($line[attributes][type])
			{
				case 'text/css':
					$result = "\t\$current_controller->helpers->html->css('{$line[attributes][href]}');";
					
//					$path_to_file = pathinfo($path_to_file);
//					$_SESSION[test] = $path_to_file[dirname].'/'.$line[attributes][href].'    '.$this->parsedFolder;
//					copy($path_to_file[dirname].'/'.$line[attributes][href], $this->parsedFolder.'/');
			}
			
			return $result."\n";	
		}
		
		/**
		 * Generates script tag
		 *
		 * @param array $line tag array to be parsed
		 * @return generated string
		 */
		private function GenerateScriptTag(array $line)
		{
			switch ($line[attributes][type])
			{
				case 'text/javascript':
					$result = "\t\$current_controller->helpers->html->js('{$line[attributes][src]}');";
					break;
			}
			
			return $result."\n";	
		}
		
		/**
		 * Generates title tag
		 *
		 * @param array $line tag array to be parsed
		 * @return generated string
		 */
		private function GenerateTitleTag(array $line)
		{
			$result = "\t\$current_controller->helpers->html->TitleTag(LANG_PROJECT_NAME.' - '.\$page_title);";
		
			return $result."\n";	
		}
	}
?>